package com.yuyou.fn.common.util;

import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by wens on 16/10/20.
 */
public class Utils {

    public static List<Long> toLongIds(String idsStr) {

        if (StringUtils.isEmpty(idsStr)) {
            return Collections.EMPTY_LIST;
        }

        String[] idsArr = idsStr.split(",");
        if (idsArr.length == 0) {
            return Collections.emptyList();
        }
        List<Long> ids = new ArrayList<>();
        for (String c : idsArr) {
            ids.add(Long.valueOf(c.trim()));
        }
        return ids;
    }

    public static byte[] s2b(String string) {
        return string == null ? null : string.getBytes(Charset.forName("UTF8"));
    }

    public static String b2s(byte[] bytes) {
        return bytes == null ? null : new String(bytes, Charset.forName("UTF8"));
    }

    public static byte[] l2b(Long along) {
        return s2b(String.valueOf(along));
    }

    public static Long b2l(byte[] bytes) {
        String string = b2s(bytes);
        return string == null ? null : Long.parseLong(string);
    }

    public static String leftPaddingZero(Long number, int len) {
        StringBuilder sb = new StringBuilder();
        sb.append(number);
        while (sb.length() < len) {
            sb.insert(0, "0");
        }
        return sb.toString();
    }


    public static String formatDate(Date date, String pattern) {
        return new SimpleDateFormat(pattern).format(date);
    }


    private static String[] chineseOfWeek = {"日", "一", "二", "三", "四", "五", "六"};

    public static String formatTwoDateDefault(Date dateStart, Date dateEnd) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");


        String d1 = dateFormat.format(dateStart);
        String d2 = dateEnd == null ? "" : dateFormat.format(dateEnd);

        String t1 = timeFormat.format(dateStart);
        String t2 = dateEnd == null ? "" : timeFormat.format(dateEnd);

        StringBuilder sb = new StringBuilder(50);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateStart);

        if (d1.equals(d2)) {
            sb.append(d1).append("(周").append(chineseOfWeek[calendar.get(Calendar.DAY_OF_WEEK) - 1]).append(")").append(t1).append("-").append(t2);
        } else {
            sb.append(d1).append("(周").append(chineseOfWeek[calendar.get(Calendar.DAY_OF_WEEK) - 1]).append(")").append(t1);
            if (dateEnd != null) {
                calendar.setTime(dateEnd);
                sb.append("-").append(d2).append("(周").append(chineseOfWeek[calendar.get(Calendar.DAY_OF_WEEK) - 1]).append(")").append(t2);
            }

        }
        return sb.toString();
    }


    public static String formatFileSize(int size) {
        return size < 1024 ? String.valueOf(size) : size / 1024 < 1024 ? String.format("%dK", size / 1024) : String.format("%dM", size / 1024 / 1024);
    }

    public static int length(Object value) {
        if (value == null) {
            return 0;
        }
        if (value instanceof Collection) {
            return ((Collection) value).size();
        } else if (value.getClass().isArray()) {
            return Array.getLength(value);
        } else if (value instanceof Map) {
            return ((Map) value).size();
        } else if (value instanceof String) {
            return ((String) value).length();
        } else {
            throw new UnsupportedOperationException("Can not get length for the object : " + value);
        }
    }

    public static String formatBigDecimal(BigDecimal value, String format) {
        if (value == null) {
            return format;
        }
        return new DecimalFormat(format).format(value);
    }

    public static long percent(Long a, Long b) {

        if (a == 0 || b == 0) {
            return 0;
        }
        return (long) (100.00 * a / b);
    }

    public static String urlEncode(String srcUrl) throws UnsupportedEncodingException {
        return URLEncoder.encode(srcUrl, "utf-8");
    }

    public static boolean isZero(BigDecimal value) throws UnsupportedEncodingException {
        return (long) (value.doubleValue() * 100) == 0l;
    }

    /**
     *
     * @param amount
     * @param numerator 分子
     * @param denominator 分母
     * @return
     */
    public static BigDecimal calProportion(BigDecimal amount , BigDecimal numerator , BigDecimal denominator ){
        return amount.multiply(numerator.divide(denominator , 10 , BigDecimal.ROUND_DOWN )) ;
    }

    public static void main(String[] args) {
        System.out.println(calProportion ( new BigDecimal(1) , new BigDecimal(4) , new BigDecimal(3) ) );
    }

    public static String camelCase(String src ){
        /*
		 * 处理下划线分割命名字段
		 */
        StringBuffer sb = new StringBuffer(src.length());
        String[] fields = src.split("_");
        sb.append(fields[0].toLowerCase());
        for (int i = 1; i < fields.length; i++) {
            String temp = fields[i];
            sb.append(temp.substring(0, 1).toUpperCase());
            sb.append(temp.substring(1).toLowerCase());
        }
        return sb.toString();
    }

}
